package  
{
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.filters.*;
	
	/**
	 * ...
	 * @author patsup
	 */
	public class AchievMenu extends Menu 
	{
		//menu image parameters
		private const COLOR:int = 0x8000FF;
		private const WIDTH:int = 800;
		private const HEIGHT:int = 480;
		
		private const MENU_BUTTON_X_STARTLOC:int = 500;			//Button for return back to menu
		private const MENU_BUTTON_Y_STARTLOC:int = 420;
		
		private var txtLabel:TextField;
		private var txtFormat:TextFormat;
		private var txtInstructions:TextField;
		
		private var mainRef:Main;
		
		private var menuButtonImgData:ImageData;
		private var menuButton:MenuButton;						//return to start menu button
		
		private var achievementList:Vector.<AchievementObject>;	//list of achievements
		public var achievements:Achievements;
		private var ObtainedFilter:GlowFilter;
		private var LockedFilter:BlurFilter;
		private var AchievementsScreen:AchievementScreen;
		
		private var achDesc:NotificationOverlay;
		
		public function AchievMenu(mainRef:Main) 
		{
			txtLabel = new TextField();
			txtInstructions = new TextField();
			txtFormat = new TextFormat();
			this.mainRef = mainRef;
			
			
			AchievementsScreen = new AchievementScreen();
			addChild(AchievementsScreen);
			
			//Here, we need to have a for loop that makes all these clickable
			for (var i:int = 1; i < 32; i++)
			{
				//AchievementsScreen.getChildByName("ach_" + i).addEventListener(MouseEvent.CLICK, displayAchDesc);
			}
			
			achDesc = new NotificationOverlay();
			
			//menuButtonImgData = new ImageData(MENU_BUTTON_X_STARTLOC, MENU_BUTTON_Y_STARTLOC, "Back");
			//menuButton = new MenuButton(menuButtonImgData, MenuBtnInputMouseClick);
			ObtainedFilter  = new GlowFilter();
			LockedFilter  = new BlurFilter();
			LockedFilter.blurX = 20;
			LockedFilter.blurY = 20;
			//LockedFilter.quality = 100;
			
			//txtLabel.text = "Achievements Menu";
			//txtInstructions.text = "Click an achievement for more information";
			//txtFormat.size = 24;
			//txtInstructions.setTextFormat(txtFormat);
			//txtInstructions.x = 50;
			//txtInstructions.y = 360;
			//txtInstructions.width = txtInstructions.textWidth*1.2;
			//txtInstructions.height = txtInstructions.textHeight;
			//addChild(txtInstructions);
			//
			//addChild(txtLabel);
			//addChild(menuButton);
			//
			//graphics.beginFill(COLOR);
			//graphics.drawRect(0, 0, WIDTH, HEIGHT);
			//graphics.endFill();
			//
			achievements = mainRef.GetAchievements();
			achievementList = achievements.GetAchievementList();
			
			AchievementsScreen.main_button.addEventListener(MouseEvent.CLICK, MenuBtnInputMouseClick);
			
			Refresh();
		}
		
		//here, we need to listen to the caller and display the appropriate acheivement data
		private function displayAchDesc(e:MouseEvent):void 
		{
			
			Refresh();
			addChild(achDesc);
			achDesc.visible = true;
			achDesc.addEventListener(MouseEvent.CLICK, hideAchDesc);
			
			var _ref:int = int(e.currentTarget.name.substr(4));
			//trace(_ref);
			
			achDesc.bust_icon.visible = false;
			achDesc.ach_icon.gotoAndStop(_ref);
			
			achDesc.header.text = achievementList[_ref-1].GetName();
			achDesc.desc.text = achievementList[_ref-1].GetDesc();
			achDesc.second.text = "";//"From round: " + achievementList[_ref-1].GetRoundAchieved();
		}
		
		private function hideAchDesc(e:MouseEvent):void
		{
			e.currentTarget.removeEventListener(MouseEvent.CLICK, hideAchDesc);
			e.currentTarget.visible = false;
		}
		
		//OnXMLLoad()
		//need to wait until xml's loaded before we get the achievements list
		//public function OnXMLLoad():void
		//{
			//achievements = mainRef.GetAchievements();
			//achievementList = achievements.GetAchievementList();
		//}
		
		//Refresh()
		//Initializes/refreshes the image of the Menu
		public function Refresh():void
		{
			//achievements.UpdateListAppearance();
			//for (var i:int = 0; i < achievementList.length; i++)
			//{
				//addChild(achievementList[i]);
			//}
			for (var i:int = 0; i < 31; i++)
			{
				AchievementsScreen["ach_" + (i + 1)].filters = [LockedFilter];
				AchievementsScreen["ach_" + (i + 1)].removeEventListener(MouseEvent.CLICK, displayAchDesc);
				//AchievementsScreen["ach_" + (_currentAch + 1)].visible = false;
				
			}
			for (i = 0; i <= achievements.achievementsReceived.length - 1; i++)
			{
				var _currentAch:int = achievements.achievementList.indexOf(achievements.achievementsReceived[i]);
				AchievementsScreen["ach_" + (_currentAch + 1)].filters = [ObtainedFilter];
				AchievementsScreen["ach_" + (_currentAch + 1)].addEventListener(MouseEvent.CLICK, displayAchDesc);
			}
			
		}
		
		//MenuBtnInputMouseClick()
		//called on back menu button mouse click
		protected function MenuBtnInputMouseClick(e:MouseEvent):void
		{
			mainRef.DisplayNewRound(this);
		}
	}

}